package com.maozzi.web.controller.rest.back;

import com.maozzi.api.acl.AclService;
import com.maozzi.api.acl.model.PermissionDTO;
import com.maozzi.api.acl.model.RoleDTO;
import com.maozzi.api.logger.annotation.SysLogger;
import com.maozzi.common.model.PageDTO;
import com.maozzi.common.model.PageParam;
import com.maozzi.common.model.PageVO;
import com.maozzi.common.model.ResultVO;
import com.maozzi.common.util.CloneUtil;
import com.maozzi.common.util.ParamCheckUtil;
import com.maozzi.web.auth.annotation.RequirePermissions;
import com.maozzi.web.auth.util.SsoUtil;
import com.maozzi.web.model.back.PermissionVO;
import com.maozzi.web.model.back.RoleVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * acl controller，权限码、角色管理
 *
 * @author maozi
 */
@Slf4j
@RestController
@RequestMapping("/rest/back/acl")
@RequirePermissions(code = "acl-code")
public class AclController {

    @Resource
    private AclService aclService;

    /**
     * 角色分页列表
     *
     * @param vo 查询条件
     * @return 返回
     */
    @GetMapping("role/page.json")
    public PageVO<RoleVO> rolePage(RoleVO vo, PageParam pageParam) {
        RoleDTO roleDTO = CloneUtil.to(vo, RoleDTO.class);
        PageDTO<RoleDTO> dto = aclService.rolePage(roleDTO, pageParam);

        return CloneUtil.toPageVO(dto, RoleVO.class);
    }

    /**
     * 添加角色
     *
     * @param vo 参数
     * @return 返回
     */
    @SysLogger(message = "添加角色")
    @PostMapping("role/add.json")
    public ResultVO<Boolean> addRole(@RequestBody RoleVO vo) {
        ParamCheckUtil.notNull(vo, "参数错误");
        ParamCheckUtil.notBlank(vo.getName(), "角色名称为空");

        RoleDTO dto = CloneUtil.to(vo, RoleDTO.class);
        String username = SsoUtil.get().getName();
        dto.setCreateBy(username);
        aclService.addRole(dto);

        return ResultVO.success(true);
    }

    /**
     * 编辑角色
     *
     * @param vo 参数
     * @return 返回
     */
    @SysLogger(message = "编辑角色")
    @PostMapping("role/edit.json")
    public ResultVO<Boolean> editRole(@RequestBody RoleVO vo) {
        ParamCheckUtil.notNull(vo, "参数错误");
        ParamCheckUtil.notNull(vo.getId(), "角色id为空");

        RoleDTO dto = CloneUtil.to(vo, RoleDTO.class);
        String username = SsoUtil.get().getName();
        dto.setModifiedBy(username);
        Boolean b = aclService.editRole(vo.getId(), dto);

        return ResultVO.success(b);
    }

    /**
     * 删除角色
     *
     * @param vo 参数
     * @return 返回
     */
    @SysLogger(message = "删除角色")
    @PostMapping("role/del.json")
    public ResultVO<Boolean> delRole(@RequestBody RoleVO vo) {
        ParamCheckUtil.notNull(vo, "参数错误");
        ParamCheckUtil.notNull(vo.getId(), "角色id为空");

        Boolean b = aclService.delRole(vo.getId());

        return ResultVO.success(b);
    }

    /**
     * 角色分页列表
     *
     * @param vo 查询条件
     * @return 返回
     */
    @GetMapping("permission/page.json")
    public PageVO<PermissionVO> permissionPage(PermissionVO vo, PageParam pageParam) {
        PermissionDTO dto = CloneUtil.to(vo, PermissionDTO.class);

        PageDTO<PermissionDTO> pageDTO = aclService.permissionPage(dto, pageParam);

        return CloneUtil.toPageVO(pageDTO, PermissionVO.class);
    }

    /**
     * 添加权限码
     *
     * @param vo 参数
     * @return 返回
     */
    @SysLogger(message = "添加权限码")
    @PostMapping("permission/add.json")
    public ResultVO<Boolean> addPermission(@RequestBody PermissionVO vo) {
        ParamCheckUtil.notNull(vo, "参数错误");
        ParamCheckUtil.notBlank(vo.getCode(), "权限码为空");

        PermissionDTO dto = CloneUtil.to(vo, PermissionDTO.class);
        String username = SsoUtil.get().getName();
        dto.setCreateBy(username);
        aclService.addPermission(dto);

        return ResultVO.success(true);
    }

    /**
     * 编辑权限码
     *
     * @param vo 参数
     * @return 返回
     */
    @SysLogger(message = "编辑权限码")
    @PostMapping("permission/edit.json")
    public ResultVO<Boolean> editPermission(@RequestBody PermissionVO vo) {
        ParamCheckUtil.notNull(vo, "参数错误");
        ParamCheckUtil.notNull(vo.getId(), "权限码id为空");

        PermissionDTO dto = CloneUtil.to(vo, PermissionDTO.class);
        String username = SsoUtil.get().getName();
        dto.setModifiedBy(username);
        Boolean b = aclService.editPermission(vo.getId(), dto);

        return ResultVO.success(b);
    }

    /**
     * 删除权限码
     *
     * @param vo 参数
     * @return 返回
     */
    @SysLogger(message = "删除权限码")
    @PostMapping("permission/del.json")
    public ResultVO<Boolean> delPermission(@RequestBody PermissionVO vo) {
        ParamCheckUtil.notNull(vo, "参数错误");
        ParamCheckUtil.notNull(vo.getId(), "角色id为空");

        Boolean b = aclService.delPermission(vo.getId());

        return ResultVO.success(b);
    }

}
